#!/usr/bin/env python
# -*- coding: utf8 -*-


''' 
# =======
# User: luwei
# Date: 11-4-29
# Time: 上午10:04
# email = luw2007@gmail.com
# =======

需要安装 tesseract-ocr3 的 tesseract 命令和imagemagick 中的 convert 命令
tesseract-ocr 3.0.0+svn581-1ppa1~na Command line OCR tool
imagemagick           7:6.6.2.6-1ubuntu4    image manipulation programs

'''

# Imports
# =======
from post_url_data import url_data ,post_url_data
#import subprocess
import  os
import sys
from BeautifulSoup import BeautifulSoup

cookie_file1 = "tmp.miit-bca.cookie"
cookie_file2 = "/dev/null"
Num = 0

def change_time(list_time):
    '''
    for ((i=$1;i>0;i--))
    do
    echo -e  ""\\c
    grep -o ">.*ICP.*号" /tmp/tmp.bca.utf8.htm |sed -n "$i"p |sed "s/>/<br>$domain,/g"
    #改变时间格式
    oldtime=`grep bxy /tmp/tmp.bca.utf8.htm| grep CST |sed -n "$i"p |grep -o "[A-Z].*[0-9]"`
    #grep bxy /tmp/tmp.bca.utf8.htm| grep CST |sed -n "$ip" |grep -o "[A-Z].*[0-9]"
    newtime=`date '+%Y-%m-%d' -d "$oldtime"`
    #echo "--------$i------$oldtime-------$newtime"
    sed -i "s/$oldtime/$newtime/" /tmp/tmp.bca.utf8.htm
    done
    '''
    month_dict = {"Jan":1,"Feb":2,"Mar":3,"Apr":4, "May":5, "Jun":6, "Jul":7,"Aug":8,"Sep":9,"Oct":10,"Nov":11,"Dec":12}
    if month_dict.get(list_time[1]):
        return '.'.join((str(list_time[5]), str(month_dict.get(list_time[1])),str(list_time[2])))


def get_miit_cookie(type):
    '''
    /usr/bin/curl -s -D  /tmp/tmp.bca.cookie http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_showPage.action >/dev/null
    #echo $?
    #[ $? -gt "0" ] && exit 1
    '''
    if type == 'bj':
        url = "http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_showPage.action"
    elif type == 'all':
        url = "http://www.miibeian.gov.cn/icp/publish/query/icpMemoInfo_showPage.action"
    else:
        print "error"
        sys.exit(1)
    html = url_data(url,cookie_file1,cookie_file2)
    if html:
        return html

def get_verify_Code(type):
    '''
    echo start miitcode  >>  /tmp/tmp.bca.log
    /usr/bin/curl -s -b  /tmp/tmp.bca.cookie http://bcainfo.miitbeian.gov.cn/validateCode >  /tmp/tmp.bca.jpg
    if [ -f  /tmp/tmp.bca.jpg ] ; then
    convert -compress none -crop 77x15+2+2 -colorspace Gray  /tmp/tmp.bca.jpg  /tmp/tmp.bca.tif
    tesseract   /tmp/tmp.bca.tif /tmp/tmp.bca 2> /dev/null
    verifyCode=`head -1   /tmp/tmp.bca.txt |sed -e 's/://' -e 's/;//' -e 's/—/-/g' -e 's/S/8/' |bc `
    echo " $domain <p> <img src=\" /tmp/tmp.bca.jpg \">$verifyCode <p> "
    #echo  `head -1  /tmp/tmp.bca.txt |sed -e 's/://' -e 's/S/8/' -e 's/;//' -e 's/—/-/g'` = $verifyCode  >> /tmp/tmp.bca.log
    #return $verifyCode
    fi
    '''
    global Num
    if  Num >= 5 :
        sys.exit(1)
    if type == 'bj':
        url = "http://bcainfo.miitbeian.gov.cn/validateCode"
        file_txt = "tmp.miit-bca"
        output_jpg = "tmp.miit-bca.jpg"
        output_tif = "tmp.miit-bca.tif"
        file_txt = "tmp.miit-bca"
    elif type == 'all':
        url = "http://www.miitbeian.gov.cn/validateCode"
        output_jpg = "tmp.miit-all.jpg"
        output_tif = "tmp.miit-all.tif"
        file_txt = "tmp.miit-all"
    else:
        print "error"
        sys.exit(1)

    get_miit_cookie(type)
    jpg = url_data(url,cookie_file1,cookie_file2)
    with open(output_jpg,'wb') as f:
        f.write(jpg)

    #subprocess.call(["/usr/bin/convert","-compress","none","-crop","77x15+2+2","-colorspace","Gray","","tmp.miit-bca.jpg","","tmp.miit-bca.tif"])
    #subprocess.Popen(["/usr/bin/tesseract","tmp.miit-bca.tif","2"], stdout=subprocess.PIPE, shell=True).communicate()
    #subprocess.call(["/usr/bin/tesseract","tmp.miit-bca.tif","1"],shell=True)

    os.system(str('convert -compress none -crop 77x15+2+2 -colorspace Gray ' + output_jpg +" "+ output_tif))
    os.system(str('/usr/bin/tesseract '+ output_tif +" "+ file_txt +"2>/dev/null "))
    
    with open(str(file_txt+".txt"),'rb') as f:
        s = f.readline()
        try:
            verify_code= eval(s[:-2])
        except :
            get_verify_Code(type)
        print s[:-2]," = ",verify_code
    Num += 1

    return verify_code




def rm_all_file(type):
    if type == 'bj':
        pass
    elif type == 'all':
        pass
    else:
        print "error"
        sys.exit(1)


def get_miit_beian(type,domain,verify_code):
    '''
    curl -s -b   /tmp/tmp.bca.cookie  -d "condition=1&siteDomain=$domain&verifyCode=$verifyCode" http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_searchExecute.action >  /tmp/tmp.bca.html
    #enconv  /tmp/tmp.bca.html
    /usr/bin/iconv -f GBK -t utf8 -o /tmp/tmp.bca.utf8.htm  /tmp/tmp.bca.html
    fromdos /tmp/tmp.bca.utf8.htm
    beiannum=`grep '>&nbsp;[0-9]\{1,3\}&nbsp;</span>条记录&nbsp'  /tmp/tmp.bca.utf8.htm |grep -o '[0-9]\{1,3\}' `
    #echo beiannum =$beiannum

    if [ ! -z "$beiannum" ] ; then
        if [ "$beiannum" -gt "0" ]; then
        changetime $beiannum
        else
                echo "<br>$domain 没有备案"
        fi
        pagenum=`cat -n /tmp/tmp.bca.utf8.htm |grep "/style" |grep -o "[0-9]\{3\}"`
        cat /tmp/tmp.bca.utf8.htm |sed -n '100,$p' |grep -B 1000 "/style"
        exit
    fi
    '''
    if type == 'bj':
        url = "http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_searchExecute.action"
    elif type == 'all':
        url = "http://www.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_searchExecute.action"
    else:
        print "error"
        sys.exit(1)

    post_data_dict = {"condition":"1","siteDomain":domain,"verifyCode":verify_code}
    url = "http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_searchExecute.action"
    html = post_url_data(url,post_data_dict,cookie_file1)
    soup = BeautifulSoup(''.join(str(html.decode('gbk').encode('utf8'))))
    #print soup
    if '验证码超时或计算错误'.decode('utf8') in soup.findAll('table')[-3].text :
        print domain,"验证码超时或计算错误"
        verify_code = get_verify_Code(type)
    if '没有符合条件的记录'.decode('utf8') in soup.findAll('table')[-3].text :
        print domain,"没有符合条件的记录"
        sys.exit(1)
    if '请仔细填写您所要查询的内容'.decode('utf8') in soup.findAll('table')[-1].find('td').text:
        print domain,"请仔细填写您所要查询的内容"
        rm_all_file(type)
        sys.exit(1)
    try:
        num = int(str(soup.find('span',{"class":"red"})).split("&nbsp")[1][1:])
    except :
        pass

    soup_1 = BeautifulSoup(''.join(str(soup.find('table',width="100%", border="0", cellspacing="0", cellpadding="0"))))
    for i in xrange(num):
        list_time = soup_1.findAll('tr')[i+1].findAll('td')[6].text.replace("&nbsp;","").split(' ')
        time=change_time(list_time)
        print soup_1.findAll('tr')[i+1].findAll('td')[0].text.replace("&nbsp;",".") ,\
        soup_1.findAll('tr')[i+1].findAll('td')[3].text.replace("&nbsp;","") ,\
        soup_1.findAll('tr')[i+1].findAll('td')[1].text.replace("&nbsp;","") ,\
        soup_1.findAll('tr')[i+1].findAll('td')[4].text.replace("&nbsp;","") ,\
        time ,\
        soup_1.findAll('tr')[i+1].findAll('td')[5].text.replace("&nbsp;","") ,\
        soup_1.findAll('tr')[i+1].findAll('td')[2].text.replace("&nbsp;","") 

        #for k in xrange(6):
        #    print soup_1.findAll('tr')[i+1].findAll('td')[k+1].text.replace("&nbsp;",""),
        #print i, soup.findAll('table')[1].findAll('tr')[i].text()

        #for i in xrange(len(soup.findAll('table'))):
        #    print soup_1.findAll('tr')[1]

def get_miit_beian_input():
    '''    echo Content-type: text/html
    echo
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"

    domain=`echo $QUERY_STRING|sed -e  's/webdomain=//g' -e 's/ //g' -e 's/\t//g'  -e 's/+//g' -e 's/^www.//g' `
    if [  -z "$domain" ] ;then
        echo "<p> 没有输入域名"
        exit 0
    fi

    echo "备案信息来自于<A href=http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_showPage.action target=_blank >bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_showPage.action<A><br> "
    #计算总时间

    date_start=$(date '+%s.%N')

    if [ -f /tmp/tmp.bca.txt ] ; then
        verifyCode=`head -1   /tmp/tmp.bca.txt |sed -e 's/://' -e 's/;//' -e 's/—/-/g' -e 's/S/8/' |bc `
    else
        [ -f  /tmp/tmp.bca.cookie ] || getmiitcookie
        getverifyCode
    fi

    NUM="1"
    NMU="1"
    while true; do

    curl -s -b   /tmp/tmp.bca.cookie  -d "condition=1&siteDomain=$domain&verifyCode=$verifyCode" http://bcainfo.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_searchExecute.action >  /tmp/tmp.bca.html
    #enconv  /tmp/tmp.bca.html
    /usr/bin/iconv -f GBK -t utf8 -o /tmp/tmp.bca.utf8.htm  /tmp/tmp.bca.html
    fromdos /tmp/tmp.bca.utf8.htm
    beiannum=`grep '>&nbsp;[0-9]\{1,3\}&nbsp;</span>条记录&nbsp'  /tmp/tmp.bca.utf8.htm |grep -o '[0-9]\{1,3\}' `
    #echo beiannum =$beiannum

    if [ ! -z "$beiannum" ] ; then
        if [ "$beiannum" -gt "0" ]; then
        changetime $beiannum
        else
                echo "<br>$domain 没有备案"
        fi
        pagenum=`cat -n /tmp/tmp.bca.utf8.htm |grep "/style" |grep -o "[0-9]\{3\}"`
        cat /tmp/tmp.bca.utf8.htm |sed -n '100,$p' |grep -B 1000 "/style"
        exit
    fi


    getverifyCode

    if [ $NUM -gt 20 ]; then
        echo "<p>获取验证码失败，请刷新再试"
        exit 0
    fi
    if [ $NMU -gt 6 ]; then
    getmiitcookie
    NMU="0"
    fi
    NUM=$[$NUM + 1]
    NMU=$[$NMU + 1]
    done

    date_end=$(date '+%s.%N')
    echo "运行了：`echo $date_end - $date_start|bc `秒"
    '''
    from optparse import OptionParser

    parser = OptionParser()
    #parser.add_option( '-d', "--domain", dest="domain", default="baidu.com", help="domain name", metavar="DOMAIN")
    parser.add_option( '-a', "--all", dest="bj", action="store_true", default=False, help="check for bj servers", metavar="bj_server")

    (options, args) = parser.parse_args()
    if  options.bj:
        type = 'bj'
        file_txt = "tmp.miit-bca.txt"
    else:
        type = 'all'
        file_txt = "tmp.miit-all.txt"
    if args:
        for domain in args:
            print "#",domain,"的备案情况"
            if os.path.isfile(file_txt):
                with open(file_txt,'rb') as f:
                    s = f.readline()
                    verify_code= eval(s[:-2])
                print s[:-2]," = " ,verify_code
            else:

                verify_code = get_verify_Code(type)
            if verify_code:
                get_miit_beian(type,domain,verify_code)
            print ""
    else:
        domain='baidu.com'
        if os.path.isfile(file_txt):
            with open(file_txt,'rb') as f:
                s = f.readline()
            try:
                verify_code= eval(s[:-2])
            except :

                get_verify_Code(type)

        else:
            verify_code = get_verify_Code(type)
        if verify_code:
            get_miit_beian(type,domain,verify_code)
        else:
            print "error"

def main():
    '''test main'''
    get_miit_beian_input()
    print "done"

if __name__ == '__main__':
    main()